/*
 * Copyright (C) 2013-2015 Bilibili
 * Copyright (C) 2013-2015 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "IJKDemoSampleViewController.h"

#import "IJKCommon.h"
#import "IJKMoviePlayerViewController.h"

@interface IJKDemoSampleViewController () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,strong) IBOutlet UITableView *tableView;
@property(nonatomic,strong) NSArray *sampleList;

@end

@implementation IJKDemoSampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"M3U8";

    NSMutableArray *sampleList = [[NSMutableArray alloc] init];
    
    [sampleList addObject:@[@"pls 1",
                            @"http://playerservices.streamtheworld.com/pls/977_COMEDY.pls"]];

    [sampleList addObject:@[@"m3u 1",
                            @"https://playerservices.streamtheworld.com/m3u/CARACOL_RADIOAAC.m3u"]];
    
    [sampleList addObject:@[@"m3u 2",
                            @"https://playerservices.streamtheworld.com/m3u/WRADIO.m3u"]];
    
    [sampleList addObject:@[@"m3u 3",
                            @"http://www.101smoothjazz.com/101-smoothjazz.m3u"]];
    
    // 能播
    [sampleList addObject:@[@"m3u8 1",
                            @"https://stream.gmanews.tv/ioslive/livestream/playlist.m3u8"]];
    
    // 能播
    [sampleList addObject:@[@"m3u8 2",
                            @"https://voa-ingest.akamaized.net/hls/live/2035206/151_124L/playlist.m3u8"]];
    
    // 能播
    [sampleList addObject:@[@"m3u8 3",
                            @"https://rfa-ingest.akamaized.net/hls/live/2040699/RFA_AUDIO_LIVE1/master.m3u8"]];
    
    // 能播
    [sampleList addObject:@[@"m3u8 4",
                            @"https://stream.gmanews.tv/ioslive/livestream/chunklist.m3u8"]];
    
    // 能播
    [sampleList addObject:@[@"mp3 1",
                            @"http://st01.dlf.de/dlf/01/128/mp3/stream.mp3"]];
    // 能播
    [sampleList addObject:@[@"mp3 2",
                            @"http://wdr-wdr5-live.icecast.wdr.de/wdr/wdr5/live/mp3/128/stream.mp3"]];
    
    // 能播
    [sampleList addObject:@[@"aac 1",
                            @"http://direct.franceinter.fr/live/franceinter-hifi.aac"]];

    [sampleList addObject:@[@"m3u",
                            @"http://playerservices.streamtheworld.com/m3u/CARACOL_RADIOAAC.m3u"]];
    
    // 能播
    [sampleList addObject:@[@"Caracol Radio Colombia (HJGL 100.9 / HJCY 810 AM, Bogotá) [AAC]",
                            @"http://27433.live.streamtheworld.com:80/CARACOL_RADIOAAC_SC"]];
    
    // 能播
    [sampleList addObject:@[@"Super Radyo DZBB 594 kHz",
                            @"https://stream.gmanews.tv/ioslive/livestream/playlist.m3u8"]];
    
    //
    [sampleList addObject:@[@"m3u",
                            @"http://playerservices.streamtheworld.com/m3u/WRADIO.m3u"]];
    
    [sampleList addObject:@[@"CCTV13",
                            @"https://piccpndali.v.myalicdn.com/audio/cctv13_2.m3u8"]];
    
    [sampleList addObject:@[@"一个音乐电台",
                            @"https://cdn.istoikona.com/ccnradio/ccnradio/icecast.audio"]];
    

    self.sampleList = sampleList;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"Samples";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (IOS_NEWER_OR_EQUAL_TO_7) {
        return self.sampleList.count;
    } else {
        return self.sampleList.count - 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"abc"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"abc"];
        cell.textLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    }

    cell.textLabel.text = self.sampleList[indexPath.row][0];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSArray *item = self.sampleList[indexPath.row];
    NSURL   *url  = [NSURL URLWithString:item[1]];

    [self.navigationController presentViewController:[[IJKVideoViewController alloc] initWithURL:url] animated:YES completion:^{}];
}

@end
